package com.cloud.wms.tools;

import com.cloud.core.base.BaseView;

import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;


/**
 * @Author： Chen
 * @Date： 2019/9/16
 * @Desc：
 */
public class RxUtils {

    private RxUtils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    /**
     * 线程切换
     *
     * @param <T>
     * @return
     */
    public static <T> ObservableTransformer<T, T> threadSwitch() {
        return observable -> observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }


    /**
     * 线程切换
     *
     * @param <T>
     * @return
     */
    public static <T> ObservableTransformer<T, T> loading(BaseView baseView) {
        return observable -> observable
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(disposable -> baseView.showLoading())
                .doFinally(baseView::hideLoading);
    }
}
